<?php
/**
 * 教育培训小程序模块小程序接口定义
 *
 * @author 武汉志汇科技
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');

class Zh_jypxModuleWxapp extends WeModuleWxapp {

	/**
	*幻灯片列表
	*/
	public function doPageGetSlide(){
		global $_GPC, $_W;
		$res = pdo_getall('snx_slide',array('status'=>'1','uniacid'=>$_W['uniacid']),array(),'','sort asc');
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}

	/**
	*头条
	**/

	public function doPageHeadline(){
		global $_GPC, $_W;
		$res = pdo_fetchall(" select * from" .tablename('snx_articles') . "where uniacid={$_W['uniacid']} and state=2 order by create_time desc");
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
	  	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}

	/**
	*获得图片路径（七牛云）
	*/
	public function doPageAttachurl(){

		global $_W;

	  echo json_encode($_W['attachurl'],320);exit();
	}


	/**
	*文章列表
	**/

	public function doPageArticles(){
		global $_GPC, $_W;
		$res = pdo_getall('snx_articles',array('uniacid'=>$_W['uniacid']));
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
	  	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}
	/**
	*文章详情
	**/
	public function doPageArticlesDetails(){
		global $_GPC, $_W;
		if (empty($_GPC['articles_id'])) {
			echo json_encode(array('msg'=>'文章参数错误','code'=>'500'),320);exit();
		}
		$res = pdo_getall('snx_articles',array('id'=>$_GPC['articles_id'],'uniacid'=>$_W['uniacid']));
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
	  	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}


	/**
	*学校列表
	**/
	public function doPageSchool(){
		global $_W,$_GPC;
		$res = pdo_getall('snx_school',array('uniacid'=>$_W['uniacid']),array('id','cate_name'));
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
	  		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();

	}
	

	/**
	*用户注册学级列表
	**/

	public function doPageCourseCate(){
		global $_GPC, $_W;
		if (isset($_GPC['school_id'])) {
			if (empty($_GPC['school_id'])) {
	  		echo json_encode(array('msg'=>'校区参数错误','code'=>'500'),320);exit();
		}else{
			$res = pdo_getall('snx_coursecate',array('school_id'=>$_GPC['school_id'],'uniacid'=>$_W['uniacid']));
			if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'502'),320);exit();
			}
	  		echo json_encode(array('msg'=>'获取成功','code'=>'201','list'=>$res),320);exit();
		  }
		}else{
			$res = pdo_fetchall("select d.id,d.course_name from" .tablename('snx_users') ." a left join " .tablename('snx_class') . " b on a.class_id=b.id left join" .tablename('snx_course') ." c on b.cou_id=c.id left join".tablename('snx_coursecate'). "d on c.c_id=d.id where a.id ={$_GPC['user_id']}" );
			if (empty($res)) {
				echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
			}
		  	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
		
	}

	/**
	*用户登陆后的学级
	**/
	public function doPageCourseCates(){
		global $_GPC, $_W;
		if (empty($_GPC['school_id'])) {
	  		echo json_encode(array('msg'=>'校区参数错误','code'=>'500'),320);exit();
		}
		// if (empty($_GPC['user_id'])) {
	 //  		echo json_encode(array('msg'=>'用户参数错误','code'=>'502'),320);exit();
		// }
		else{
			$res = pdo_fetchall("select a.course_name,a.id from" .tablename('snx_coursecate') ."a left join" .tablename('snx_school') . "b on a.school_id=b.id where b.id={$_GPC['school_id']}");
			// $res = pdo_fetchall("select d.id,d.course_name from" .tablename('snx_users') ." a left join " .tablename('snx_class') . " b on a.class_id=b.id left join " .tablename('snx_course') ." c on b.cou_id=c.id left join".tablename('snx_coursecate'). "d on c.c_id=d.id left join " . tablename('snx_school') ."e on d.school_id=e.id where a.id ={$_GPC['user_id']} and e.id={$_GPC['school_id']}" );
			// echo $sql = "select d.id,d.course_name from" .tablename('snx_users') ." a left join " .tablename('snx_class') . " b on a.class_id=b.id left join " .tablename('snx_course') ." c on b.cou_id=c.id left join".tablename('snx_coursecate'). "d on c.c_id=d.id left join " . tablename('snx_school') ."e on d.school_id=e.id where a.id ={$_GPC['user_id']} and e.id={$_GPC['school_id']}";die;
			if (empty($res)) {
				echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
			}
		  	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
	}


	/**
	*课程列表
	**/

	public function doPageCourse(){
		global $_GPC, $_W;
		if (empty($_GPC['id'])) {
	  		echo json_encode(array('msg'=>'学级参数错误','code'=>'500'),320);exit();
		}else{
			$res = pdo_getall('snx_course',array('c_id'=>$_GPC['id'],'uniacid'=>$_W['uniacid']));
			if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}

	  		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
		
	}


	/**
	*注册班级列表
	**/

	public function doPageClass(){
		global $_GPC, $_W;
		if (empty($_GPC['cou_id'])) {
	  		echo json_encode(array('msg'=>'课程参数错误','code'=>'500'),320);exit();
		}else{
			$res = pdo_getall('snx_class',array('cou_id'=>$_GPC['cou_id'],'uniacid'=>$_W['uniacid']));
			if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
	  		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
	}


	/**
	*课节列表
	**/
	public function doPageUnit(){
		global $_GPC, $_W;
		if (empty($_GPC['cour_id'])) {
	  		echo json_encode(array('msg'=>'课程参数错误','code'=>'500'),320);exit();
		}if (empty($_GPC['user_id'])) {
	  		echo json_encode(array('msg'=>'用户参数错误','code'=>'502'),320);exit();
		}else{
			$res = pdo_fetchall("SELECT a.*,b.name,c.course_name FROM ".tablename('snx_unit'). " a"  . " left join " . tablename("snx_course") . " b on a.cour_id=b.id " . " left join " . tablename("snx_coursecate") . " c on b.c_id=c.id where a.cour_id=".$_GPC['cour_id'] . " and a.uniacid=".$_W['uniacid']);
			if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
			for ($i=0; $i <count($res) ; $i++) { 
        	 	$user = pdo_fetch("select c.name from" .tablename('snx_users') ." a left join " .tablename('snx_class') . " b on a.class_id=b.id left join" .tablename('snx_course') ." c on b.cou_id=c.id where a.id ={$_GPC['user_id']}" );
        	 	$user = implode(' ', $user);
        	 	$res[$i]['subject'] = $user;
			}
			for ($i=0; $i <count($res) ; $i++) { 
				$res[$i]['zy_state'] = 1;
        	 	$task = pdo_fetchall("select b.state from" .tablename('snx_task') ." a left join " .tablename('snx_answer') . " b on a.id=b.task_id where b.u_id ={$_GPC['user_id']} and a.unit_id={$res[$i]['id']} " );
        	if($task){
	       	$state= array_column($task, 'state');
        	if(in_array('1',$state)){
        		$res[$i]['zy_state'] = 1;
        	}else{
        		$res[$i]['zy_state'] = 2;
        	}
        }
      
			}
	  		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
		
	}


	/**
	*判断用户是否可以进入当前课节
	**/
	public function doPageJudgeTask(){
		global $_GPC, $_W;
		if (empty($_GPC['unit_id'])) {
	  		echo json_encode(array('msg'=>'课节参数错误','code'=>'500'),320);exit();
		}if (empty($_GPC['user_id'])) {
	  		echo json_encode(array('msg'=>'用户参数错误','code'=>'502'),320);exit();
		}
			$row = pdo_fetchall("SELECT a.id FROM ".tablename('snx_answer'). " a"  . " left join " . tablename("snx_task"). "b on a.task_id=b.id ". " left join " . tablename("snx_unit") . " c on b.unit_id=c.id where a.u_id={$_GPC['user_id']} and a.uniacid={$_W['uniacid']}");
			$row = count($row);
        	$list = (int)pdo_fetchcolumn("SELECT count(*) FROM ".tablename('snx_task') ."where unit_id={$_GPC['unit_id']}");
        	if ($row<$list) {
        		echo json_encode(array('msg'=>'作业还未做完不能进入下一题','code'=>'503'),320);exit();
        	}else{
        		echo json_encode(array('msg'=>'进入成功','code'=>'200'),320);
        	}
	}


	/**
	*作业列表
	**/
	public function doPageTask(){
		global $_GPC, $_W;
		if (empty($_GPC['unit_id'])) {
	  		echo json_encode(array('msg'=>'课节参数错误','code'=>'500'),320);exit();
		}if (empty($_GPC['user_id'])) {
	  		echo json_encode(array('msg'=>'用户参数错误','code'=>'502'),320);exit();
		}else{
			$res = pdo_fetchall("SELECT a.*,b.unit_name,b.state,c.name,d.course_name FROM ".tablename('snx_task'). " a"  . " left join " . tablename("snx_unit"). "b on a.unit_id=b.id ". " left join " . tablename("snx_course") . " c on b.cour_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id where a.unit_id=".$_GPC['unit_id'] . " and a.uniacid=".$_W['uniacid']);
			foreach ($res as $key => $value) {
				$answer=pdo_get('snx_answer',array('task_id'=>$value['id'],'u_id'=>$_GPC['user_id']),array('answer', 'u_id'));
				$res[$key]['answer']=$answer['answer'];
				$res[$key]['u_id']=$answer['u_id'];
				
			}
			if (empty($res)) {
				echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
			}
	  		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
	}


	/**
	*作业详情
	**/
	public function doPageTaskDetali(){
		global $_GPC, $_W;
		if (empty($_GPC['task_id'])) {
	  		echo json_encode(array('msg'=>'作业参数错误','code'=>'500'),320);exit();
		}else{
			$res = pdo_fetchall("SELECT a.*,b.unit_name,c.name,d.course_name FROM ".tablename('snx_task'). " a"  . " left join " . tablename("snx_unit"). "b on a.unit_id=b.id ". " left join " . tablename("snx_course") . " c on b.cour_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id where a.id=".$_GPC['task_id'] . " and a.uniacid=".$_W['uniacid']);
			if (empty($res)) {
				echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
			}
	  		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}

	}


	/**
	*发送手机验证码
	**/
	public function doPageCode(){
	    global $_GPC,$_W;
	    include IA_ROOT.'/addons/zh_jypx/SendCode.php';
	    $res=pdo_get('snx_smsconfig',array('uniacid'=>$_W['uniacid']));
	    // var_dump($res);die;
	    $appkey=$res['appkey'];//聚合appkey
	    $phone=$_GPC['phone'];//接过来的手机号
	    $tpl_id=$res['tpl_id'];//短信模板id
	    // $str = '3456789abdefghjkmnpswqxyrt';//验证码字符串
        // $code = substr(str_shuffle($str),0,6);
        $code = $_GPC['code'];
	    $tpl_value=urlencode('#code#'."=".$code);//模板变量
	    // $tpl_value=urlencode($res['tpl_value']);//模板变量
	    if (empty($_GPC['phone'])) {
	    	echo json_encode(array('msg'=>'手机号不能为空','code'=>'500'),320);exit();
	    }
	    if (!preg_match('/^1[34578]\d{9}$/',$phone)) {
	    	echo json_encode(array('msg'=>'请输入正确的手机号','code'=>'501'),320);exit();
	    }
	    $results = new SendCode($appkey,$phone,$tpl_id,$tpl_value);
	    $return=$results->code();
	    if($return){
		    $result = json_decode($return,true);
		    $error_code = $result['error_code'];
		    if($error_code == 0){
		        //状态为0，说明短信发送成功
		        // pdo_insert('snx_code',array('code'=>$code,'phone'=>$phone,'create_time'=>date('Y-m-d H:i:s',time())));
		        echo json_encode(array('msg'=>'发送成功','code'=>$result['result']['sid']),320);exit();
		    }else{
		        //状态非0，说明失败
		        $msg = $result['reason'];
		        echo json_encode(array('msg'=>$msg,'code'=>$error_code),320);exit();
		    }
		}else{
		    //返回内容异常，以下可根据业务逻辑自行修改
		    echo "请求发送短信失败";
		}
	}



	/**
	*用户注册
	**/

	public function doPageRegister(){
		global $_GPC,$_W;
		$data['class_id'] = $_GPC['class_id'];
		$data['parents_name'] = $_GPC['parents_name'];
		$data['students_name'] = $_GPC['students_name'];
		$data['phone_number'] = $_GPC['phone'];
		$data['password'] = md5(md5($_GPC['pass_word']));
		$data['create_time'] = date('Y-m-d H:i:s',time());
		$data['uniacid'] = $_W['uniacid'];
		$data['state'] = 1;
		$data['openid']=$_GPC['openid'];
		$data['img']=$_GPC['img'];
		$data['nickname']=$_GPC['nickname'];
		if (empty($_GPC['class_id']) && $_GPC['class_id']='Undefined') {
			$info['parents_name'] = $_GPC['parents_name'];
			$info['students_name'] = $_GPC['students_name'];
			$info['phone_number'] = $_GPC['phone'];
			$info['password'] = md5(md5($_GPC['pass_word']));
			$info['create_time'] = date('Y-m-d H:i:s',time());
			$info['uniacid'] = $_W['uniacid'];
			$info['state'] = 3;
			$info['openid']=$_GPC['openid'];
			$info['img']=$_GPC['img'];
			$info['nickname']=$_GPC['nickname'];
			if (empty($_GPC['students_name'])) {
	    		echo json_encode(array('msg'=>'学生姓名不能为空','code'=>'501'),320);exit();
		    }if (empty($_GPC['phone'])) {
		    	echo json_encode(array('msg'=>'手机号不能为空','code'=>'502'),320);exit();
		    }if (empty($_GPC['code'])) {
		    	echo json_encode(array('msg'=>'验证码不能为空','code'=>'503'),320);exit();
		    }if (empty($_GPC['pass_word'])) {
		    	echo json_encode(array('msg'=>'密码不能为空','code'=>'504'),320);exit();
		    }
			$res = pdo_getall('snx_users',array('phone_number'=>$_GPC['phone']),array('phone_number'));
		    if ($res) {
		    	echo json_encode(array('msg'=>'该手机号已被注册','code'=>'505'),320);exit();
		    }else{
		    	$row = pdo_insert('snx_users',$info);
				if ($row) {
					echo json_encode(array('msg'=>'注册成功，等待审核','code'=>'200'),320);exit();
		        }else{
					echo json_encode(array('msg'=>'注册失败','code'=>'506'),320);exit();
				}
		    }
		}else{
		if (empty($_GPC['class_id'])) {
			echo json_encode(array('msg'=>'班级参数错误','code'=>'507'),320);exit();
		}if (empty($_GPC['parents_name'])) {
	    	echo json_encode(array('msg'=>'家长姓名不能为空','code'=>'500'),320);exit();
	    }if (empty($_GPC['students_name'])) {
	    	echo json_encode(array('msg'=>'学生姓名不能为空','code'=>'501'),320);exit();
	    }if (empty($_GPC['phone'])) {
	    	echo json_encode(array('msg'=>'手机号不能为空','code'=>'502'),320);exit();
	    }if (empty($_GPC['code'])) {
	    	echo json_encode(array('msg'=>'验证码不能为空','code'=>'503'),320);exit();
	    }if (empty($_GPC['pass_word'])) {
	    	echo json_encode(array('msg'=>'密码不能为空','code'=>'504'),320);exit();
	    }
	    $res = pdo_getall('snx_users',array('phone_number'=>$_GPC['phone']),array('phone_number'));
	    if ($res) {
	    	echo json_encode(array('msg'=>'该手机号已被注册','code'=>'505'),320);exit();
	    }else{
	    	$row = pdo_insert('snx_users',$data);
			if ($row) {
				echo json_encode(array('msg'=>'注册成功，等待审核','code'=>'200'),320);exit();
	        }else{
				echo json_encode(array('msg'=>'注册失败','code'=>'506'),320);exit();
			}
	    }
		}
		
			
		
		
		
	    
	}

	/**
	*用户登录
	**/

	public function doPageLogin(){
		global $_GPC,$_W;
			if (empty($_GPC['phone'])) {
	    		echo json_encode(array('msg'=>'手机号不能为空','code'=>'502'),320);exit();
		    }if (empty($_GPC['pass_word'])) {
		    	echo json_encode(array('msg'=>'密码不能为空','code'=>'504'),320);exit();
		    }
		    $row = pdo_getall('snx_users',array('phone_number'=>$_GPC['phone']),array('phone_number'));
		    if (empty($row)) {
		    	echo json_encode(array('msg'=>'该手机号尚未注册，请先注册','code'=>'505'),320);exit();
		    }
		    $roe = pdo_get("snx_users",array('phone_number'=>$_GPC['phone'],'password'=>md5(md5($_GPC['pass_word']))),array('password','phone_number'));
		    if (empty($roe)) {
		    	echo json_encode(array('msg'=>'手机号和密码不匹配','code'=>'506'),320);exit();
		    }
		    $rst = pdo_get("snx_users",array('phone_number'=>$_GPC['phone'],'password'=>md5(md5($_GPC['pass_word']))),array('state'));
		    if ($rst['state'] ==1) {
		    	echo json_encode(array('msg'=>'该用户还在审核中，请稍后重试','code'=>'507'),320);exit();
		    }else{
		    	$res = pdo_fetch("select a.id,a.phone_number,a.img,a.class_id,a.students_name,a.parents_name,a.score,b.class_name,c.name,d.course_name,e.cate_name,e.id as schoolid,e.cate_name,e.phone,e.address from ".tablename('snx_users'). " a"  . " left join " . tablename("snx_class"). "b on a.class_id=b.id ". " left join " . tablename("snx_course") . " c on b.cou_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id" . " left join " . tablename('snx_school') . "e on d.school_id=e.id where a.phone_number=".$_GPC['phone'] . " and a.uniacid=" .$_W['uniacid']);
		    	$res['type'] =1;
		    	if (empty($res)) {
					echo json_encode(array('msg'=>'暂无数据','code'=>'508'),320);exit();
				}
              	
				$islogin=pdo_get('snx_users',array('phone_number'=>$_GPC['phone']));
              	if($islogin['login']==1){
                  echo json_encode(array('msg'=>'用户已登录','code'=>'509'),320);exit();
                }else{
                  
                  $user_data = array('login' => 1,);
                  pdo_update('snx_users',$user_data,array('phone_number'=>$_GPC['phone']));
                  echo json_encode(array('msg'=>'登录成功','code'=>'200','data'=>$res),320);exit();
                }
                
		    }

	}
  	public function doPageLoginstate(){
      global $_GPC,$_W;
      			if (empty($_GPC['phone'])) {
	    		echo json_encode(array('msg'=>'手机号不能为空','code'=>'502'),320);exit();
                }if (empty($_GPC['pass_word'])) {
                    echo json_encode(array('msg'=>'密码不能为空','code'=>'504'),320);exit();
                }
      			$row = pdo_getall('snx_users',array('phone_number'=>$_GPC['phone']),array('phone_number'));
                if (empty($row)) {
                    echo json_encode(array('msg'=>'该手机号尚未注册，请先注册','code'=>'505'),320);exit();
                }
                $roe = pdo_get("snx_users",array('phone_number'=>$_GPC['phone'],'password'=>md5(md5($_GPC['pass_word']))),array('password','phone_number'));
                if (empty($roe)) {
                    echo json_encode(array('msg'=>'手机号和密码不匹配','code'=>'506'),320);exit();
                }
                $rst = pdo_get("snx_users",array('phone_number'=>$_GPC['phone'],'password'=>md5(md5($_GPC['pass_word']))),array('state'));
                if ($rst['state'] ==1) {
                    echo json_encode(array('msg'=>'该用户还在审核中，请稍后重试','code'=>'507'),320);exit();
                }
				$islogin=pdo_get('snx_users',array('phone_number'=>$_GPC['phone']));
              	if($islogin['login']==1){
                  echo json_encode(array('msg'=>'用户已登录','code'=>'508'),320);exit();
                }else{
                  
                  $user_data = array('login' => 1,);
                  pdo_update('snx_users',$user_data,array('phone_number'=>$_GPC['phone']));
                  echo json_encode(array('msg'=>'登录成功','code'=>'200','data'=>$res),320);exit();
                }
		    

	}

	/**
	*判断用户是否登录
	**/

	public function doPageIsLogin(){
		global $_GPC,$_W;
		if (empty($_GPC['u_id'])) {
			echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}
		$list = pdo_getall('snx_users',array('id'=>$_GPC['u_id']));
	    if (empty($list)) {
	    	echo json_encode(array('msg'=>'该用户不存在，请先注册','code'=>'501'),320);exit();
	    }else{
	    	echo json_encode(array('msg'=>'获取用户成功','code'=>'200'),320);exit();
	    }
	}

	/**
	*获取openid
    */
    public function doPageOpenid(){
		global $_W, $_GPC;
	  $res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
	   	$code=$_GPC['code'];
	   	$appid=$res['appid'];
	   	$secret=$res['appsecret'];
	   	//var_dump($secret);die;
   		$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code";
		function httpRequest($url,$data = null){ 
           $curl = curl_init();
           curl_setopt($curl, CURLOPT_URL, $url);
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
           curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
           if (!empty($data)){
               curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
           //执行
           $output = curl_exec($curl);
           curl_close($curl);
            return $output;
          }
         $res=httpRequest($url);
	   	 print_r($res);
	}

	/**
	*重置密码
	**/

	public function doPageResetPassword(){
		global $_GPC,$_W;
		$data['password'] = md5(md5($_GPC['npass_word']));
		if (empty($_GPC['phone'])) {
	    	echo json_encode(array('msg'=>'手机号不能为空','code'=>'502'),320);exit();
	    }if (empty($_GPC['code'])) {
	    	echo json_encode(array('msg'=>'验证码不能为空','code'=>'503'),320);exit();
	    }if (empty($_GPC['npass_word'])) {
	    	echo json_encode(array('msg'=>'密码不能为空','code'=>'504'),320);exit();
	    }
		$res = pdo_getall('snx_users',array('phone_number'=>$_GPC['phone']),array('phone_number'));
	    if (empty($res)) {
	    	echo json_encode(array('msg'=>'该手机号尚未注册，请先注册','code'=>'505'),320);exit();
	    }
	    $list = pdo_update('snx_users',array('password'=>$data['password'],'login'=>0),array('phone_number'=>$_GPC['phone']));
	    if ($list) {
	    	echo json_encode(array('msg'=>'重置密码成功','code'=>'200'),320);exit();
	    }else{
	    	echo json_encode(array('msg'=>'重置密码失败','code'=>'507'),320);exit();
	    }
	}



	/**
	*提交作业
	**/
	public function doPageSubmitTask(){
		global $_GPC,$_W;
      
      	function getaccess_token($_W){
          $res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }
        function set_msgs($_W,$res_id){
            $access_token = getaccess_token($_W);
            $ress=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
            $openid=pdo_get('snx_users',array('id'=>$res_id));
            $class=pdo_get('snx_class',array('id'=>$openid['class_id']));
          	$teacher = pdo_fetchall(" select * from" .tablename('snx_teacher') . "where uniacid={$_W['uniacid']} and FIND_IN_SET( {$openid['class_id']}, class_id) order by id desc");
          	var_dump($teacher);die;
            $formwork ='{
                "touser": "'.$teacher[0]["openid"].'",
                "template_id": "'.$ress["templeidworkup"].'",
                "form_id":"'.$openid['wformid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$openid['students_name'].'",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"'.$class['class_name'].'",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
      
		if (empty($_GPC['select'])) {
		echo json_encode(array('msg'=>'提交作业不能为空','code'=>'500'),320);exit();
		}
		$a=json_decode(html_entity_decode($_GPC['select']));
		$select=json_decode(json_encode($a),true);
      	$wformid = array(
            'wformid' =>$_GPC['wformid'],
        );
      	pdo_update('snx_users',$wformid,array('uniacid'=>$_W['uniacid'],'id'=>$select[0]['u_id']));
      	set_msgs($_W,$select[0]['u_id']);
		foreach ($select as $key => $value) {
			$data['task_id']=$value['id'];
			$data['u_id']=$value['u_id'];
			$data['answer']=$value['value'];
			$data['answer_time']=$value['answer_time'];
			$data['create_time']=date('Y-m-d H:i:s',time());
			$data['uniacid']=$_W['uniacid'];
			$data['state']=1;
			$data['status']=1;
			$res = pdo_insert('snx_answer',$data,array('uniacid'=>$_W['uniacid']));
			if (empty($res)) {
	    	echo json_encode(array('msg'=>'提交作业失败，请重新提交','code'=>'501'),320);
			}else{
		    	echo json_encode(array('msg'=>'提交作业成功','code'=>'200'),320);
			}
		}
	}


	/**
	*更正作业
	**/

	public function doPageModifyTask(){
		global $_GPC,$_W;
		if (empty($_GPC['select'])) {
		echo json_encode(array('msg'=>'提交作业不能为空','code'=>'500'),320);exit();
		}
		$a=json_decode(html_entity_decode($_GPC['select']));
		$select=json_decode(json_encode($a),true);
		foreach ($select as $key => $value) {
			//$data['task_id']=$value['id'];
			//$data['u_id']=$value['u_id'];
			$data['answer']=$value['value'];
			$data['answer_time']=$value['answer_time'];
			$data['create_time']=date('Y-m-d H:i:s',time());
			$data['uniacid']=$_W['uniacid'];
			$data['state']=1;
			$res = pdo_update('snx_answer',$data,array('uniacid'=>$_W['uniacid'],'id'=>$value['id'],'u_id'=>$value['u_id']));
			if (empty($res)) {
		    	echo json_encode(array('msg'=>'修改作业失败，请重新提交','code'=>'501'),320);exit();
			}else{
		    	echo json_encode(array('msg'=>'修改作业成功','code'=>'200'),320);
		    	// pdo_update('snx_answer',array('state'=>1),array('task_id'=>$value['id'],'u_id'=>$value['u_id']));
			}
		}
	}

	/**
	 * 奖品列表
	 */
	

	public function doPagePrize(){
		global $_GPC,$_W;
		$res =pdo_getall('snx_prize',array('uniacid'=>$_W['uniacid']));
		if (empty($res)) {
	    	echo json_encode(array('msg'=>'暂无奖品','code'=>'500'),320);exit();
		}else{
	    	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
	}

	/**
	*奖品详情
	**/

	public function doPagePrizeDetails(){
		global $_GPC,$_W;
		if (empty($_GPC['prize_id'])) {
			echo json_encode(array('msg'=>'奖品参数错误','code'=>'500'),320);exit();
		}
		$res =pdo_get('snx_prize',array('uniacid'=>$_W['uniacid'],'id'=>$_GPC['prize_id']));
		if (empty($res)) {
	    	echo json_encode(array('msg'=>'暂无奖品','code'=>'501'),320);exit();
		}else{
	    	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		}
	}


	/**
	*我的作业
	**/


	public function doPageMyTask(){
		global $_W,$_GPC;
		if (empty($_GPC['userid'])) {
	    	echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}if (empty($_GPC['state'])) {
	    	echo json_encode(array('msg'=>'状态参数错误','code'=>'502'),320);exit();
		}
		$res = pdo_fetchall("SELECT b.*,a.*,c.unit_name,e.course_name FROM ".tablename('snx_answer'). " a"  . " left join " . tablename("snx_task") . " b on a.task_id=b.id" . " left join " . tablename("snx_unit") . " c on b.unit_id=c.id" . " left join " . tablename("snx_course") . " d on c.cour_id=d.id" . " left join " . tablename("snx_coursecate") . " e on d.c_id=e.id where a.u_id=".$_GPC['userid'] . " and a.uniacid=".$_W['uniacid']. " and a.state=".$_GPC['state']);
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}

	/**
	*我的题目列表
	**/
	public function doPageMyTaskList(){
		global $_W,$_GPC;
		if (empty($_GPC['userid'])) {
	    	echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}if (empty($_GPC['unitid'])) {
	    	echo json_encode(array('msg'=>'课节参数错误','code'=>'501'),320);exit();
		}if (empty($_GPC['status'])) {
	    	echo json_encode(array('msg'=>'状态参数错误','code'=>'502'),320);exit();
		}
		$res = pdo_fetchall("SELECT a.*,b.unit_id,b.title,b.thumb,b.video,b.describe,b.voice,c.unit_name,d.name,e.course_name FROM ".tablename('snx_answer'). " a"  . " left join " . tablename("snx_task") . " b on a.task_id=b.id" . " left join " . tablename("snx_unit") . " c on b.unit_id=c.id" . " left join " . tablename("snx_course") . " d on c.cour_id=d.id" . " left join " . tablename("snx_coursecate") . " e on d.c_id=e.id where a.status={$_GPC['status']} and a.u_id=".$_GPC['userid'] . " and a.uniacid=".$_W['uniacid'] . " and b.unit_id=" .$_GPC['unitid']);
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'504'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}



	/**
	*我的题目详情
	**/
	public function doPageMyTaskDetails(){
		global $_W,$_GPC;
		if (empty($_GPC['taskid'])) {
	    	echo json_encode(array('msg'=>'作业参数错误','code'=>'501'),320);exit();
		}
		if (empty($_GPC['status'])) {
	    	echo json_encode(array('msg'=>'状态参数错误','code'=>'502'),320);exit();
		}
		$res = pdo_fetchall("SELECT a.*,b.unit_id,b.title,b.thumb,b.video,b.describe,b.voice,c.unit_name,d.name,e.course_name FROM ".tablename('snx_answer'). " a"  . " left join " . tablename("snx_task") . " b on a.task_id=b.id" . " left join " . tablename("snx_unit") . " c on b.unit_id=c.id" . " left join " . tablename("snx_course") . " d on c.cour_id=d.id" . " left join " . tablename("snx_coursecate") . " e on d.c_id=e.id where a.status={$_GPC['status']} and a.uniacid=".$_W['uniacid'] . " and a.id=" .$_GPC['taskid']);
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}

	

	/**
	*我的资料
	*/
	public function doPageMyInformation(){
		global $_GPC, $_W;
		if (empty($_GPC['user_id'])) {
			echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}
		$res = pdo_fetch("select a.phone_number,a.students_name,a.parents_name,b.class_name,c.name,d.course_name,e.cate_name from ".tablename('snx_users'). " a"  . " left join " . tablename("snx_class"). "b on a.class_id=b.id ". " left join " . tablename("snx_course") . " c on b.cou_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id" . " left join " . tablename('snx_school') . "e on d.school_id=e.id where a.uniacid={$_W['uniacid']} and a.id ={$_GPC['user_id']}");
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		
	}
	/**
	*我的积分
	**/
	public function doPageMyScores(){
		global $_GPC, $_W;
		if (empty($_GPC['user_id'])) {
			echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}
		$res = pdo_get('snx_users',array('id'=>$_GPC['user_id']),'score');
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		
	}


	/**
	*我的积分明细
	**/
	public function doPageMyScoreDetails(){
		global $_GPC, $_W;
		if (empty($_GPC['user_id'])) {
			echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}
		$res = pdo_fetchall("select a.*,d.name,e.course_name from ".tablename('snx_scoredetails'). " a left join " . tablename("snx_task"). "b on a.task_id=b.id left join " . tablename("snx_unit") . " c on b.unit_id=c.id left join" .tablename('snx_course') ." d on c.cour_id=d.id left join" .tablename('snx_coursecate') ." e on d.c_id=e.id where a.uniacid={$_W['uniacid']} and a.u_id={$_GPC['user_id']}");
		for($i= 0;$i<count($res);$i++){
			$row = pdo_get('snx_users',array('id'=>$_GPC['user_id']),array('score'));
			$res[$i]['totalscore'] = $row['score'];
		}
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
		
	}


	/**
	*排名
	**/

	public function doPageRanking(){
		global $_W,$_GPC;
		if (empty($_GPC['user_id'])) {
			echo json_encode(array('msg'=>'用户参数错误','code'=>'500'),320);exit();
		}
      	$classid=pdo_get('snx_users',array('id'=>$_GPC['user_id']),array('class_id'));
      //echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$classid['class_id']),20);exit();
		$res = pdo_fetchall("select a.students_name,a.score,b.class_name,c.name,d.course_name,e.cate_name from ".tablename('snx_users'). " a"  . " left join " . tablename("snx_class"). "b on a.class_id=b.id ". " left join " . tablename("snx_course") . " c on b.cou_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id" . " left join " . tablename('snx_school') . "e on d.school_id=e.id where a.uniacid=" .$_W['uniacid'] . " and a.class_id=" .$classid['class_id'] . " order by score desc");
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
		}
	    	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}



	/**
	*老师登录
	**/
	public function doPageTeacherLogin(){
		global $_GPC,$_W;
		if (empty($_GPC['username'])) {
	    	echo json_encode(array('msg'=>'用户名不能为空','code'=>'500'),320);exit();
	    }if (empty($_GPC['password'])) {
	    	echo json_encode(array('msg'=>'密码不能为空','code'=>'501'),320);exit();
	    }
	    $row = pdo_getall('snx_teacher',array('username'=>$_GPC['username']),array('username'));
	    if (empty($row)) {
	    	echo json_encode(array('msg'=>'该用户尚未注册，请联系管理员','code'=>'502'),320);exit();
	    }
	    $roe = pdo_getall("snx_teacher",array('username'=>$_GPC['username'],'password'=>md5(md5($_GPC['password']))),array('password','username'));
	    if (empty($roe)) {
	    	echo json_encode(array('msg'=>'用户名和密码不匹配','code'=>'503'),320);exit();
	    }else{
	    	$res = pdo_fetch("select a.id,a.username,a.teacher_name,a.class_id,a.state,b.class_name,c.name,d.course_name,e.cate_name from ".tablename('snx_teacher'). " a"  . " left join " . tablename("snx_class"). "b on a.class_id=b.id ". " left join " . tablename("snx_course") . " c on b.cou_id=c.id " . " left join " . tablename("snx_coursecate") . " d on c.c_id=d.id" . " left join " . tablename('snx_school') . "e on d.school_id=e.id where a.state=1 and a.username='{$_GPC['username']}' and a.uniacid=" . $_W['uniacid']);
	    	$res['type']= 0;
	    	if (empty($res)) {
				echo json_encode(array('msg'=>'暂无数据','code'=>'504'),320);exit();
			}
          	pdo_update('snx_teacher',$openid,array('uniacid'=>$_W['uniacid'],'username'=>$_GPC['username']));
          	$islogin=pdo_get('snx_teacher',array('username'=>$_GPC['username']));
          	if($islogin['login']==1){
              echo json_encode(array('msg'=>'用户已登录','code'=>'508'),320);exit();
            }else{
              $user_data = array('login' => 1,);
              pdo_update('snx_teacher',$user_data,array('username'=>$_GPC['username']));
              echo json_encode(array('msg'=>'登录成功','code'=>'200','data'=>$res),320);exit();
            }
	    }
	}
  	public function doPageTeacherLoginstate(){
      			global $_GPC,$_W;
      			if (empty($_GPC['username'])) {
                    echo json_encode(array('msg'=>'用户名不能为空','code'=>'500'),320);exit();
                }if (empty($_GPC['password'])) {
                    echo json_encode(array('msg'=>'密码不能为空','code'=>'501'),320);exit();
                }
                $row = pdo_getall('snx_teacher',array('username'=>$_GPC['username']),array('username'));
                if (empty($row)) {
                    echo json_encode(array('msg'=>'该用户尚未注册，请联系管理员','code'=>'502'),320);exit();
                }
                $roe = pdo_getall("snx_teacher",array('username'=>$_GPC['username'],'password'=>md5(md5($_GPC['password']))),array('password','username'));
                if (empty($roe)) {
                    echo json_encode(array('msg'=>'用户名和密码不匹配','code'=>'503'),320);exit();
                }
				$islogin=pdo_get('snx_teacher',array('username'=>$_GPC['username']));
              	if($islogin['login']==1){
                  echo json_encode(array('msg'=>'用户已登录','code'=>'508'),320);exit();
                }else{
                  
                  $user_data = array('login' => 1,);
                  pdo_update('snx_teacher',$user_data,array('username'=>$_GPC['username']));
                  echo json_encode(array('msg'=>'登录成功','code'=>'200','data'=>$res),320);exit();
                }
		    

	
    }



	/**
	*老师学级列表
	**/
	public function doPageCateList(){
		global $_GPC,$_W;
		if (empty($_GPC['teacher_id'])) {
			echo json_encode(array('msg'=>'教师参数错误','code'=>'500'),320);exit();
		}
		$teacher = pdo_get('snx_teacher',array('id'=>$_GPC['teacher_id']),array('class_id'));
		$roe = $teacher['class_id'];
	 	$class = pdo_fetchall("select cou_id from " .tablename('snx_class')."where id in ($roe)");
	 	foreach ($class as $v) {  
	        $v = join(",",$v); // 可以用implode将一维数组转换为用逗号连接的字符串，join是别名  
	        $temp[] = $v;  
    	}
	    foreach ($temp as $v) {  
	        $t.=$v.",";  
	    }
	    $t = substr($t, 0, -1); // 利用字符串截取函数消除最后一个逗号
	 	$cate = pdo_fetchall("select c_id from " .tablename('snx_course')."where id in ($t)");

	 	foreach ($cate as $a) {  
	        $a = join(",",$a); // 可以用implode将一维数组转换为用逗号连接的字符串，join是别名  
	        $test[] = $a;  
    	}  
	    foreach ($test as $a) {  
	        $b.=$a.",";  
	    }
	    $b = substr($b, 0, -1); // 利用字符串截取函数消除最后一个逗号
	    $coursecate = pdo_fetchall("select id,course_name from " .tablename('snx_coursecate')."where id in ($b)");
		if (empty($coursecate)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'501'),320);exit();
	 	}
			echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$coursecate),320);exit();
	}


	/**
	*老师课程列表
	**/
	public function doPageCourseList(){
		global $_GPC,$_W;
		if (empty($_GPC['course_id'])) {
			echo json_encode(array('msg'=>'学级参数错误','code'=>'501'),320);exit();
		}
		
		$res = pdo_getall('snx_course',array('c_id'=>$_GPC['course_id'],'uniacid'=>$_W['uniacid']));
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'502'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();

	}


	/**
	*老师班级列表
	**/

	public function doPageClassList(){
		global $_GPC,$_W;
      	
		if (empty($_GPC['cou_id'])) {
			echo json_encode(array('msg'=>'课程参数错误','code'=>'501'),320);exit();
		}
      	$class_id=pdo_get('snx_teacher',array('id'=>$_GPC['teacher_id']));
		$arr = explode(",",$class_id['class_id']);
		foreach ($arr as $value) {
			$res[] = pdo_fetch("SELECT a.*,b.name,c.course_name FROM ".tablename('snx_class'). " a"  . " left join " . tablename("snx_course") . " b on a.cou_id=b.id " . " left join " . tablename("snx_coursecate") . " c on b.c_id=c.id where a.cou_id=".$_GPC['cou_id']." and a.id=".$value);
		
        }
		//$res = pdo_fetchall("SELECT a.*,b.name,c.course_name FROM ".tablename('snx_class'). " a"  . " left join " . tablename("snx_course") . " b on a.cou_id=b.id " . " left join " . tablename("snx_coursecate") . " c on b.c_id=c.id where a.cou_id=".$_GPC['cou_id'] . " and a.uniacid=".$_W['uniacid']);
		//$res = pdo_fetchall("SELECT a.*,b.name,c.course_name FROM ".tablename('snx_class'). " a"  . " left join " . tablename("snx_course") . " b on a.cou_id=b.id " . " left join " . tablename("snx_coursecate") . " c on b.c_id=c.id where a.cou_id=".$_GPC['cou_id'] . " and a.id=".$_GPC['teacher_id']);
       // $res = pdo_fetchall("SELECT a.*,b.name,c.course_name,d.id FROM ".tablename('snx_class'). " a"  . " left join " . tablename("snx_course") . " b on a.cou_id=b.id " . " left join " . tablename("snx_coursecate") . " c on b.c_id=c.id"."left join".tablename("snx_teacher")."d on c.id=d.c_id where a.cou_id=".$_GPC['cou_id'] . " and d.id=".$_GPC['teacher_id']);
      	if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();

	}


	/**
	*老师作业列表
	**/

	public function doPageTaskList(){
		global $_GPC,$_W;
		if (empty($_GPC['class_id'])) {
			echo json_encode(array('msg'=>'班级参数错误','code'=>'501'),320);exit();
		}if (empty($_GPC['status'])) {
			echo json_encode(array('msg'=>'状态参数错误','code'=>'502'),320);exit();
		}
		$res = pdo_fetchall("select a.state,a.status,b.*,c.students_name,c.id as userid from " .tablename('snx_answer') . " a " ." left join " .tablename('snx_task') . " b on a.task_id = b.id " . "left join" .tablename('snx_users') . "c on a.u_id = c.id where a.status ={$_GPC['status']} and a.state =1 and c.class_id ={$_GPC['class_id']} and a.uniacid ={$_W['uniacid']} group by c.students_name");
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}


	/**
	*老师作业答案详情
	**/
	public function doPageAnswer(){
		global $_GPC,$_W;
		if (empty($_GPC['task_id'])) {
			echo json_encode(array('msg'=>'作业参数错误','code'=>'501'),320);exit();
		}if (empty($_GPC['userid'])) {
			echo json_encode(array('msg'=>'参数错误','code'=>'502'),320);exit();
		}
		$res = pdo_fetchall("select a.id as answer_id,a.answer,a.answer_time,b.*,c.students_name,c.id as users_id from " .tablename('snx_answer') . " a " ." left join " .tablename('snx_task') . " b on a.task_id = b.id " . "left join" .tablename('snx_users') . "c on a.u_id = c.id where a.task_id ={$_GPC['task_id']} and a.uniacid ={$_W['uniacid']} and a.u_id={$_GPC['userid']} ");
		if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}
	/**
	*老师作业答案详情s
	**/
	public function doPageAnswers(){
		global $_GPC,$_W;
      
		if (empty($_GPC['userid'])) {
			echo json_encode(array('msg'=>'参数错误','code'=>'502'),320);exit();
		}
        $res = pdo_fetchall("select a.id as answer_id,a.answer,a.status,b.*,c.students_name,c.id as users_id from " .tablename('snx_answer') ."a " ." left join " .tablename('snx_task') . " b on a.task_id = b.id " . "left join" .tablename('snx_users') . "c on a.u_id = c.id where a.uniacid ={$_W['uniacid']} and a.status ={$_GPC['status']} and a.state =1 and a.u_id={$_GPC['userid']} ");
        
      	if (empty($res)) {
			echo json_encode(array('msg'=>'暂无数据','code'=>'500'),320);exit();
		}
		echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
	}

	/**
	*老师批改作业
	**/

	public function doPageCorrectTask(){
		global $_W,$_GPC;
		function getaccess_token($_W){
          $res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }
        function set_msgss($_W,$res_id,$tea_pho,$task_id){
            $access_token = getaccess_token($_W);
            $ress=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
            $urses=pdo_get('snx_users',array('id'=>$res_id));
            $teacher=pdo_get('snx_teacher',array('username'=>$tea_pho));
			$tasks=pdo_get('snx_task',array('id'=>$task_id));
            $formwork ='{
                "touser": "'.$teacher["openid"].'",
                "template_id": "'.$ress["templeidworkover"].'",
                "form_id":"'.$urses['oformid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$teacher['teacher_name'].'",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"'.$tasks['title'].'",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
      	$oformid = array(
            'oformid' =>$_GPC['oformid'],
        );
      	pdo_update('snx_users',$oformid,array('uniacid'=>$_W['uniacid'],'id'=>$_GPC['userid']));
      	set_msgss($_W,$_GPC['userid'],$_GPC['teacherphone'],$_GPC['task_id']);
		if (empty($_GPC['answer_id'])) {
			echo json_encode(array('msg'=>'答案参数错误','code'=>'501'),320);exit();
		}if (empty($_GPC['status'])) {
			echo json_encode(array('msg'=>'状态参数错误','code'=>'502'),320);exit();
		}

		$stateee=pdo_get('snx_answer',array('task_id'=>$_GPC['task_id']),array('task_id', 'status', 'state'));
		$res = pdo_update('snx_answer',array('status'=>$_GPC['status'],'state'=>'2','t_answer'=>$_GPC['recive_con'],'t_answer_time'=>$_GPC['t_answer_time']),array('id'=>$_GPC['answer_id'],'uniacid'=>$_W['uniacid']));
		if (empty($res)) {
			echo json_encode(array('msg'=>'批改失败','code'=>'500'),320);exit();
		}else{
			//查找用户
			$user=pdo_get('snx_answer',array('id'=>$_GPC['answer_id']),'u_id');
			echo json_encode(array('msg'=>'批改成功','code'=>'1000'),320);
			// if ($_GPC['status'] == 3 && $stateee['state']==1) {
			// 	$info['task_id'] = $_GPC['task_id'];
			// 	$info['u_id'] = $user['u_id'];
			// 	$info['score'] = 2;
			// 	$info['create_time'] = date('Y-m-d H:i:s',time());
			// 	$info['uniacid'] = $_W['uniacid'];
			// 	pdo_insert('snx_scoredetails',$info);
			// }
			if ($_GPC['status'] == 2) {
				if ($stateee['status'] == 3 && $stateee['state']==1) {
					$info['task_id'] = $_GPC['task_id'];
					$info['u_id'] = $user['u_id'];
					$info['score'] = 2;
					$info['create_time'] = date('Y-m-d H:i:s',time());
					$info['uniacid'] = $_W['uniacid'];
					pdo_insert('snx_scoredetails',$info);
				}else{
					$data['task_id'] = $_GPC['task_id'];
					$data['u_id'] = $user['u_id'];
					$data['score'] = 10;
					$data['create_time'] = date('Y-m-d H:i:s',time());
					$data['uniacid'] = $_W['uniacid'];
					pdo_insert('snx_scoredetails',$data);
				}
			}if ($_GPC['status'] == 3 && $stateee['state']==2) {
				$info['task_id'] = $_GPC['task_id'];
				$info['u_id'] = $user['u_id'];
				$info['score'] = 0;
				$info['create_time'] = date('Y-m-d H:i:s',time());
				$info['uniacid'] = $_W['uniacid'];
				pdo_insert('snx_scoredetails',$info);
			}
			$rew = pdo_get("snx_scoredetails",array('u_id'=>$user['u_id']),array('sum(score) as score'));
			pdo_update('snx_users',array('score'=>$rew['score']),array('id'=>$user['u_id']));exit();
		}
	}



	/**
	*上传录音
	*/

	public function doPageUploadFiles(){
	    global $_W, $_GPC;
	 $uptypes=array(
        'audio/wav',
        'audio/mp3',
        'audio/wma',
        'audio/amr',
        'audio/cd',
        'audio/wave',
        'audio/mpeg',
        'audio/mpeg-4'
        );
    $max_file_size=2000000;     //上传文件大小限制, 单位BYTE
    $destination_folder="../attachment/"; //上传文件路径
    // echo $_FILES["upfile"]['type'];exit();
    if (!is_uploaded_file($_FILES["upfile"]['tmp_name']))
    //是否存在文件
    {
         echo json_encode(array('msg'=>'录音不存在','code'=>'500'),320);exit();
    }
    $file = $_FILES["upfile"];
    if($max_file_size < $file["size"])
    //检查文件大小
    {
         echo json_encode(array('msg'=>'文件太大','code'=>'501'),320);exit();
    }
    if(!in_array($file["type"], $uptypes))
    //检查文件类型
    {
         echo json_encode(array('msg'=>'文件类型不符合','code'=>'502','type'=>$file["type"]),320);exit();
        
    }
    $filename=$file["tmp_name"];
    $image_size = getimagesize($filename);
    $pinfo=pathinfo($file["name"]);
    $ftype=$pinfo['extension'];
    $destination = $destination_folder.str_shuffle(time().rand(111111,999999)).".".$ftype;
    if (file_exists($destination) && $overwrite != true)
    {
        echo json_encode(array('msg'=>'文件已经存在','code'=>'502'),320);exit();
    }
    if(!move_uploaded_file ($filename, $destination))
    {
        echo json_encode(array('msg'=>'移动文件出错','code'=>'503'),320);exit();
    }
    $pinfo=pathinfo($destination);
    $fname=$pinfo['basename'];
    $fname = str_replace(PHP_EOL, "",$fname);
    echo $fname;
    @require_once (IA_ROOT . '/framework/function/file.func.php');
    @$filename=$fname;
    @file_remote_upload($filename);

}

//以下核销管理

//添加核销人员二维码生成
public function doPageHxCode(){
	global $_W, $_GPC;
	load()->func('tpl');
	function  getCoade($apply_id){
		function getaccess_token(){
			global $_W, $_GPC;
			$res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
			$appid=$res['appid'];
			$secret=$res['appsecret'];			
          // $appid="wx648013d2ed95099f";
          // $secret="37a58d68f3e94cffd2fea40c5add670a";
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
			$data = curl_exec($ch);
			curl_close($ch);
			$data = json_decode($data,true);
			return $data['access_token'];
		}
		function set_msg($apply_id){
			$access_token = getaccess_token();
			$data2=array(
				"scene"=>$apply_id,
				//"page"=>"zh_jypx/pages/teacher/btn",
				"width"=>100
				);
			$data2 = json_encode($data2);
			$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token."";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
			curl_setopt($ch, CURLOPT_POST,1);
			curl_setopt($ch, CURLOPT_POSTFIELDS,$data2);
			$data = curl_exec($ch);
			curl_close($ch);
			return $data;
		}
		$img=set_msg($apply_id);
		$img=base64_encode($img);
		return $img;
	}
	echo getCoade($_GPC['dh_id']);
} 

/**
*兑换奖品
**/
public function doPageExchangePrizes(){
	global $_GPC,$_W;
	$data['user_id'] = $_GPC['user_id'];
	$data['prize_id'] = $_GPC['prize_id'];
	$data['user_name'] = $_GPC['user_name'];
	$data['score'] = $_GPC['score'];
	// $data['num'] = $_GPC['num'];
	$data['prize_name'] = $_GPC['prize_name'];
	$data['prize_img'] = $_GPC['prize_img'];
	$data['user_tel'] = $_GPC['user_tel'];
	$data['time'] = date("Y-m-d H:i:s",time());
	$data['uniacid'] = $_W['uniacid'];
	$data['state'] = 1;
	if (empty($_GPC['prize_id'])) {
		echo json_encode(array('msg'=>'奖品参数错误','code'=>'500'),320);exit();
	}
	$roe = pdo_get('snx_users',array('id'=>$_GPC['user_id']),array('score'));

	if ($roe['score']<$_GPC['score']) {
		echo json_encode(array('msg'=>'兑换失败，您的积分不足以兑换该奖品','code'=>'501'),320);exit();
	}
	// $prizeres = pdo_get('snx_prize',array('id'=>$_GPC['prize_id']),array('num'));
	// if($prizeres['num']==0){
	// 	echo  json_encode(array('msg'=>'奖品数量不足','code'=>'503','prizeres'=>$prizeres),320);exit();
	// }
	// $isexist = pdo_get('snx_exchange',array('user_id'=>$_GPC['user_id']),array('prize_id'));
	// if($isexist){
	// 	$isexistp = pdo_get('snx_exchange',array('user_id'=>$_GPC['user_id'],'prize_id'=>$_GPC['prize_id']),array('prize_id'));
	// 	if($isexistp){
	// 	echo json_encode(array('msg'=>'同一个奖品只能兑一次','code'=>'504','isexist'=>$isexist),320);exit();
	// 	}
	// }
	$res = pdo_insert('snx_exchange',$data,array('uniacid'=>$_W['uniacid']));
	if (empty($res)) {
		echo json_encode(array('msg'=>'兑换失败，请重试','code'=>'502'),320);exit();
	}else{
		echo json_encode(array('msg'=>'兑换成功','code'=>'200'),320);
		pdo_update('snx_prize',array('num -='=>1),array('id'=>$_GPC['prize_id']));
		$score = $roe['score'] - $_GPC['score'];
		pdo_update('snx_users',array('score'=>$score),array('id'=>$_GPC['user_id']));
		//兑换成功并添加积分记录
		$data2['type'] = 2;
		$data2['prize_id'] = $_GPC['prize_id'];
		$data2['u_id'] = $_GPC['users_id'];
		$data2['score'] = $_GPC['score'];
		$data2['create_time'] = date('Y-m-d H:i:s',time());
		$data2['uniacid'] = $_W['uniacid'];
		pdo_insert('snx_scoredetails',$data2);exit();
	}

}


//核销奖品
public function doPageIsHx(){
	global $_W, $_GPC;
 	//查找所有核销员
	$hxer=pdo_getall('snx_hx',array('uniacid'=>$_W['uniacid']),'hx_id');
	$hx_id = array_column($hxer, 'hx_id');
	if (in_array($_GPC['teacher_id'], $hx_id))
	{
		$rst=pdo_update('snx_exchange',array('state'=>2,'hx_id'=>$_GPC['teacher_id'],'dh_time'=>date('Y-m-d H:i:s',time())),array('id'=>$_GPC['dh_id']));
		if($rst){
			echo json_encode(array('msg'=>'核销成功','code'=>'200','list'=>$rst),320);exit();
		}else{
			echo json_encode(array('msg'=>'核销失败，请重试','code'=>'502'),320);exit();
		}
	
	}
	else
	{
		echo json_encode(array('msg'=>'无权核销，请联系管理员','code'=>'502'),320);exit();
	}

}


//兑换列表
public function doPageMyExchangeRecord(){
	global $_GPC, $_W;
	$pageindex = max(1, intval($_GPC['page']));
	$pagesize=10;
	$data[':uniacid']=$_W['uniacid'];
	$data[':user_id']=$_GPC['user_id'];

	$where=" where a.uniacid=:uniacid and a.user_id=:user_id ";
	$sql="select a.*,b.name,b.pic  from " . tablename("snx_exchange")." a left join ".tablename('snx_prize')." b on a.prize_id=b.id" .$where." order by a.id desc ";
	$select_sql=$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
	$list=pdo_fetchall($select_sql,$data);
	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$list),320);exit();

}

//兑换详情
public function doPageExchangeDetails(){
	global $_GPC, $_W;
	$res=pdo_get('snx_exchange',array('id'=>$_GPC['dh_id']));
	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
}



//判断当前学生所在的学级
public function doPageGetUserCourse(){
	global $_GPC, $_W;
	$sql="SELECT d.course_name FROM ".tablename('snx_users'). " a left join " . tablename("snx_class") . " b on a.class_id=b.id left join" .tablename('snx_course') ."c on b.cou_id=c.id left join".tablename('snx_coursecate') ."d on c.c_id=d.id where a.id={$_GPC['student_id']}";
	$res=pdo_fetch($sql);
	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();
}


//获取兑换规则
public function doPageGetDhRule(){
	global $_GPC, $_W;
	$res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']),'dy_details');
	echo json_encode(array('msg'=>'获取成功','code'=>'200','list'=>$res),320);exit();

}


//判断是否可以领取
public function doPageCheckLq(){
	global $_GPC, $_W;
	$prize=pdo_get('snx_exchange',array('user_id'=>$_GPC['user_id'],'prize_id'=>$_GPC['prize_id']));
	if ($prize) {
		echo json_encode(array('msg'=>'兑换失败，不能重复兑换该奖品','code'=>'501'),320);exit();
	}
	$score = pdo_get('snx_users',array('id'=>$_GPC['user_id']),array('score'));
	if ($score['score']<$_GPC['score']) {
		echo json_encode(array('msg'=>'兑换失败，您的积分不足以兑换该奖品','code'=>'501'),320);exit();
	}
	//生成奖品二维码
	load()->func('tpl');
	function  getCoade($prize_id,$user_id){
		function getaccess_token(){
			global $_W, $_GPC;
			$res=pdo_get('snx_system',array('uniacid'=>$_W['uniacid']));
			$appid=$res['appid'];
			$secret=$res['appsecret'];			
          // $appid="wx648013d2ed95099f";
          // $secret="37a58d68f3e94cffd2fea40c5add670a";
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
			$data = curl_exec($ch);
			curl_close($ch);
			$data = json_decode($data,true);
			return $data['access_token'];
		}
		function set_msg($prize_id,$user_id){
			$access_token = getaccess_token();
			$data2=array(
				"scene"=>$prize_id.",".$user_id,
				//"page"=>"zh_jypx/pages/teacher/btn",,
				"width"=>100
				);
			$data2 = json_encode($data2);
			$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token."";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
			curl_setopt($ch, CURLOPT_POST,1);
			curl_setopt($ch, CURLOPT_POSTFIELDS,$data2);
			$data = curl_exec($ch);
			curl_close($ch);
			return $data;
		}
		$img=set_msg($prize_id,$user_id);
		$img=base64_encode($img);
		return $img;
	}
	echo getCoade($_GPC['prize_id'],$_GPC['user_id']);
	
}


//领取奖品

public function doPageSaveLqPrize(){
	global $_W, $_GPC;
 	//查找所有核销员
	$hxer=pdo_getall('snx_hx',array('uniacid'=>$_W['uniacid']),'hx_id');
	$hx_id = array_column($hxer, 'hx_id');
	if (in_array($_GPC['hx_id'], $hx_id)){
	$prizeInfo=pdo_get('snx_prize',array('id'=>$_GPC['prize_id'],'uniacid'=>$_W['uniacid']));
	//保存奖品记录
	$data['user_id'] = $_GPC['user_id'];
	$data['prize_id'] = $_GPC['prize_id'];
	$data['user_name'] = $_GPC['user_name'];
	$data['score'] = $prizeInfo['need_score'];
	$data['hx_id'] = $_GPC['hx_id'];
	$data['prize_name'] = $prizeInfo['name'];
	$data['prize_img'] = $prizeInfo['pic'];
	$data['user_tel'] = $_GPC['user_tel'];
	$data['time'] = date("Y-m-d H:i:s",time());
	$data['uniacid'] = $_W['uniacid'];
	$data['state'] = 2;
	$res = pdo_insert('snx_exchange',$data);
	//减奖品
	pdo_update('snx_prize',array('num -='=>1),array('id'=>$_GPC['prize_id']));
	//减积分
	$rst=pdo_update('snx_users',array('score -='=>$prizeInfo['need_score']),array('id'=>$_GPC['user_id']));
	if($rst){
		$data2['type'] = 2;
		$data2['prize_id'] = $_GPC['prize_id'];
		$data2['u_id'] = $_GPC['user_id'];
		$data2['score'] = $prizeInfo['need_score'];
		$data2['create_time'] = date('Y-m-d H:i:s',time());
		$data2['uniacid'] = $_W['uniacid'];
		pdo_insert('snx_scoredetails',$data2);exit();
	}

	}else{
		echo json_encode(array('msg'=>'无权核销，请联系管理员','code'=>'502'),320);exit();
	}

}

	/**
	*用户退出
	**/

public function doPageChild_retrun(){
  				global $_W, $_GPC;
 				$islogin=pdo_get('snx_users',array('phone_number'=>$_GPC['phone']));
              	if($islogin['login']==1){ 
                  $user_data = array('login' => 0,);
                  pdo_update('snx_users',$user_data,array('phone_number'=>$_GPC['phone']));
                  echo json_encode(array('msg'=>'用户已退出','code'=>'200'),320);exit();
                }else{
                  echo json_encode(array('msg'=>'退出失败','code'=>'508'),320);exit();
                }
}


	/**
	*老师退出
	**/

public function doPageTeacher_retrun(){
  				global $_W, $_GPC;
 				$islogin=pdo_get('snx_teacher',array('username'=>$_GPC['username']));
              	if($islogin['login']==1){
                  $user_data = array('login' => 0,);
                  pdo_update('snx_teacher',$user_data,array('username'=>$_GPC['username']));
                  echo json_encode(array('msg'=>'用户已退出','code'=>'200'),320);exit();
                }else{
                  
                  echo json_encode(array('msg'=>'退出失败','code'=>'508'),320);exit();
                }
}

















//////////////////////////////////////

}